package mobileann.mafamily.utils;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;

import com.mofind.android.net.HttpUtils;
import com.mofind.android.net.JSONParser;
import com.mofind.android.net.HttpUtils.HttpResultCallback;

public class EleListUtils {
    private static EleListUtils _instance;
    public static final int ADDELE_SUCCESS=751;
    public static final int ADDELE_ERROR=752;
    public static final int UPDATEELE_SUCCESS=753;
    public static final int UPDATEELE_ERROR=754;
    public static final int DELELE_SUCCESS=755;
    public static final int DELELE_ERROR=756;
    public synchronized static EleListUtils getInstance() {
	if (_instance == null) {
	    _instance = new EleListUtils();
	}
	return _instance;
    }
    private Map<String, String> getAddElePara(String target_uid,String longitude,String latitude,String radius,String desc,String enabled) {
	Map<String, String> map = new HashMap<String, String>();
	map.put("from", "add_fence");
	map.put("uid", SPUtils.getMySelf(SPUtils.MYUID));
	map.put("data", addData(target_uid, longitude, latitude, radius, desc, enabled));
	return map;
    }
    private String addData(String target_uid,String longitude,String latitude,String radius,String desc,String enabled){
	Map<String, String> map = new HashMap<String, String>();
	map.put("uid", SPUtils.getMySelf(SPUtils.MYUID));
	map.put("target_uid", target_uid);
	map.put("longitude", longitude);
	map.put("latitude", latitude);
	map.put("radius", radius);
	map.put("desc", desc);
	map.put("enabled", enabled);
	String data = MyTaskUtils.getInstance().map2json(map);
	return TripleDESUtil.CBCEncode(SPUtils.getMySelf(SPUtils.MYUID),SPUtils.getMySelf(SPUtils.MYURAD), data);
    }
    public void doAddEle(String target_uid,String longitude,String latitude,String radius,String desc,String enabled,final Handler handler){
	HttpUtils.startHttpPost(URLUtils.URL_GROWTH_UPLOAD, getAddElePara(target_uid, longitude, latitude, radius, desc,enabled), new HttpResultCallback() {
	    
	    @Override
	    public void result(String url, boolean isResult, String jsonStr) {
		// TODO Auto-generated method stub
		if(isResult){
		    String json = TripleDESUtil.getJsonObjStr(SPUtils.getMySelf(SPUtils.MYUID),
				SPUtils.getMySelf(SPUtils.MYURAD), jsonStr);
		    if(!TextUtils.isEmpty(json)){
			JSONObject jo = JSONParser.getJSONObject(json);
			String status=JSONParser.getString(jo, "status");
			if("true".equals(status)){
			    if(handler!=null){
				handler.obtainMessage(ADDELE_SUCCESS).sendToTarget();
			    }
			}else{
			    String reason=JSONParser.getString(jo, "reason");
			    if(handler!=null){
				handler.obtainMessage(ADDELE_ERROR,reason).sendToTarget();
			    }
			}
		    }else{
			 if(handler!=null){
			     handler.obtainMessage(ADDELE_ERROR,"上传数据失败").sendToTarget();
			   }
		    }
		}
	    }
	});
    }
    private Map<String, String> getUpdateElePara(String target_uid,String eleId,String longitude,String latitude,String radius,String desc,String enabled) {
	Map<String, String> map = new HashMap<String, String>();
	map.put("from", "update_fence");
	map.put("uid", SPUtils.getMySelf(SPUtils.MYUID));
	map.put("data", updateData(target_uid,eleId, longitude, latitude, radius, desc, enabled));
	return map;
    }
    private String updateData(String target_uid,String eleId,String longitude,String latitude,String radius,String desc,String enabled){
	Map<String, String> map = new HashMap<String, String>();
	map.put("uid", SPUtils.getMySelf(SPUtils.MYUID));
	map.put("target_uid", target_uid);
	map.put("fid", eleId);
	map.put("longitude", longitude);
	map.put("latitude", latitude);
	map.put("radius", radius);
	map.put("desc", desc);
	map.put("enabled", enabled);
	String data = MyTaskUtils.getInstance().map2json(map);
	return TripleDESUtil.CBCEncode(SPUtils.getMySelf(SPUtils.MYUID),SPUtils.getMySelf(SPUtils.MYURAD), data);
    }
    public void doUpdateEle(String target_uid,String eleId,String longitude,String latitude,String radius,String desc,String enabled,final Handler handler){
	HttpUtils.startHttpPost(URLUtils.URL_GROWTH_UPLOAD, getUpdateElePara(target_uid,eleId, longitude, latitude, radius,desc, enabled), new HttpResultCallback() {
	    
	    @Override
	    public void result(String url, boolean isResult, String jsonStr) {
		if(isResult){
		    String json = TripleDESUtil.getJsonObjStr(SPUtils.getMySelf(SPUtils.MYUID),
				SPUtils.getMySelf(SPUtils.MYURAD), jsonStr);
		    if(!TextUtils.isEmpty(json)){
			JSONObject jo = JSONParser.getJSONObject(json);
			String status=JSONParser.getString(jo, "status");
			if("true".equals(status)){
			    if(handler!=null){
				handler.obtainMessage(UPDATEELE_SUCCESS).sendToTarget();
			    }
			}else{
			    String reason=JSONParser.getString(jo, "reason");
			    if(handler!=null){
				handler.obtainMessage(UPDATEELE_ERROR,reason).sendToTarget();
			    }
			}
		    }else{
			 if(handler!=null){
			     handler.obtainMessage(UPDATEELE_ERROR,"上传数据失败").sendToTarget();
			   }
		    }
		}
	    }
	});
    }
    private Map<String, String> getDelElePara(String target_uid,String eleId) {
	Map<String, String> map = new HashMap<String, String>();
	map.put("from", "delete_fence");
	map.put("uid", SPUtils.getMySelf(SPUtils.MYUID));
	map.put("data", delData(target_uid,eleId));
	return map;
    }
    private String delData(String target_uid,String eleId){
	Map<String, String> map = new HashMap<String, String>();
	map.put("uid", SPUtils.getMySelf(SPUtils.MYUID));
	map.put("target_uid", target_uid);
	map.put("fid", eleId);
	String data = MyTaskUtils.getInstance().map2json(map);
	return TripleDESUtil.CBCEncode(SPUtils.getMySelf(SPUtils.MYUID),SPUtils.getMySelf(SPUtils.MYURAD), data);
    }
    public void doDelEle(String target_uid,String eleId,final Handler handler){
	HttpUtils.startHttpPost(URLUtils.URL_GROWTH_UPLOAD, getDelElePara(target_uid,eleId), new HttpResultCallback() {
	    
	    @Override
	    public void result(String url, boolean isResult, String jsonStr) {
		if(isResult){
		    String json = TripleDESUtil.getJsonObjStr(SPUtils.getMySelf(SPUtils.MYUID),
				SPUtils.getMySelf(SPUtils.MYURAD), jsonStr);
		    if(!TextUtils.isEmpty(json)){
			JSONObject jo = JSONParser.getJSONObject(json);
			String status=JSONParser.getString(jo, "status");
			if("true".equals(status)){
			    if(handler!=null){
				handler.obtainMessage(DELELE_SUCCESS).sendToTarget();
			    }
			}else if("false".equals(status)){
			    String reason=JSONParser.getString(jo, "reason");
			    if(handler!=null){
				handler.obtainMessage(DELELE_ERROR,reason).sendToTarget();
			    }
			}
		    }else{
			 if(handler!=null){
			     handler.obtainMessage(DELELE_ERROR,"上传数据失败").sendToTarget();
			 }
		    }
		}
	    }
	});
    }
}
